<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############



$THEME->addBreadcrumb($this->adminUrl.'/lang/install/', $this->lang('ap_title_lang_install'));


if(Antz_IntelliForm::submitted('new-language-form')){
    $error = false;

    $newKeyname = post('keyname');
    $copyLang = post('copyLang');

    if(strlen(trim($newKeyname))==0){
        $error = true;
        JFX::addError('ISO cannot be blank');
        JFX::addFormError('keyname');
    }
    $newKeyname = $DB->escape($newKeyname);
    if($DB->countRows($CONFIG->dbprefix.'languages', "keyname = '{$newKeyname}'")>0){
        $error = true;
        JFX::addError('ISO is already in use');
        JFX::addFormError('keyname');
    }
    $copyLang = $DB->escape($copyLang);
    if($DB->countRows($CONFIG->dbprefix.'languages', "keyname = '{$copyLang}'")==0){
        $error = true;
        JFX::addError('Invalid language to copy');
        JFX::addFormError('copyLang');
    }

    if(!$error){
        $flagDir = $CONFIG->imagesDir.'/flags/'.$newKeyname;
        if(isset($_FILES['small_img']) && file_exists($_FILES['small_img']['tmp_name'])){
            $fileinfo = fileinfo($_FILES['small_img']['tmp_name']);
            $ext = $fileinfo['extension'];
            $newFilename = $flagDir.'/small.'.$ext;
            copy($_FILES['small_img']['tmp_name'], $newFilename);
            JFX_Image::resize($newFilename, $JFX_LANG_IMAGE_SIZES['small']['width'], $JFX_LANG_IMAGE_SIZES['small']['height']);
        }
        if(isset($_FILES['med_img']) && file_exists($_FILES['med_img']['tmp_name'])){
            $fileinfo = fileinfo($_FILES['med_img']['tmp_name']);
            $ext = $fileinfo['extension'];
            $newFilename = $flagDir.'/medium.'.$ext;
            copy($_FILES['med_img']['tmp_name'], $newFilename);
            JFX_Image::resize($newFilename, $JFX_LANG_IMAGE_SIZES['med']['width'], $JFX_LANG_IMAGE_SIZES['med']['height']);
        }
        if(isset($_FILES['large_img']) && file_exists($_FILES['large_img']['tmp_name'])){
            $fileinfo = fileinfo($_FILES['large_img']['tmp_name']);
            $ext = $fileinfo['extension'];
            $newFilename = $flagDir.'/large.'.$ext;
            copy($_FILES['large_img']['tmp_name'], $newFilename);
            JFX_Image::resize($newFilename, $JFX_LANG_IMAGE_SIZES['large']['width'], $JFX_LANG_IMAGE_SIZES['large']['height']);
        }
        if(isset($_FILES['xtra_img']) && file_exists($_FILES['xtra_img']['tmp_name'])){
            $fileinfo = fileinfo($_FILES['xtra_img']['tmp_name']);
            $ext = $fileinfo['extension'];
            $newFilename = $flagDir.'/extra-large.'.$ext;
            copy($_FILES['xtra_img']['tmp_name'], $newFilename);
            JFX_Image::resize($newFilename, $JFX_LANG_IMAGE_SIZES['xtra']['width'], $JFX_LANG_IMAGE_SIZES['xtra']['height']);
        }
        $dbParams = array(
            'keyname' => $newKeyname,
            'is_active' => 1,
            'flag_extension' => $ext,
            'is_default' => 0
        );
        $DB->insert($CONFIG->dbprefix.'languages', $dbParams);

        // now we do the heavy lifting
        set_time_limit(0);
        $sysLangs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}lang_system WHERE lang = '{$copyLang}'");
        foreach($sysLangs as $k=>$v){
            $v['lang'] = $newKeyname;
            $DB->insert($CONFIG->dbprefix.'lang_system', $v);
        };

        // lets break this next task into groups of module type and workspace id
        $workspaces = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}workspaces");
        $modules = array();
        $dh = opendir($CONFIG->modulesDir);
        while($file = readdir($dh)){
            if($file=='.' || $file=='..' || !is_dir($CONFIG->modulesDir.'/'.$file)) continue;
            $modules[] = $file;
        }

        foreach($workspaces as $ws){
            $wsid = $ws['id'];
            foreach($modules as $module){
                $langContent = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}lang_content WHERE lang = '{$copyLang}' AND workspace_id = '{$wsid}' AND module_key = '{$module}'");
                foreach($langContent as $k=>$v){
                    $v['lang'] = $newKeyname;
                    $DB->insert($CONFIG->dbprefix.'lang_content', $v);
                }
            }
        }

        unset($langContent, $workspaces, $modules);

        JFX::addSuccess('The language has been copied, you may now translate it');
        JFX::redirect($CONFIG->adminUrl.'/lang/translate/?lang='.$newKeyname);

    }

}



$langs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages ORDER BY keyname ASC");
$copyLangOpts = array();
foreach($langs as $k=>$v){
    $copyLangOpts[$v['keyname']] = $v['keyname'];
}
$newLangFormFields = array(
    array(
        'type' => 'text',
        'name' => 'keyname',
        'value' => post('keyname', 'en_US'),
        'label' => 'ISO Language Code'
    ),
    array(
        'type' => 'select',
        'name' => 'copyLang',
        'label' => 'Language to copy',
        'options' => $copyLangOpts,
        'value' => post('copyLang', 'en_US')
    ),
    array(
        'type' => 'submit',
        'label' => '&nbsp;',
        'value' => 'Create Language'
    )
);

$newLangForm = JFX::makeRapidForm($newLangFormFields, 'New Language Details', 'new-language-form');



echo $newLangForm;






